package com.xyoto.community.service.impl;


import com.xyoto.community.constant.OperationStatus;
import com.xyoto.community.constant.OssBucketModuleName;
import com.xyoto.community.constant.RotationChartShowStatus;
import com.xyoto.community.dto.AddSelectedRotationDTO;
import com.xyoto.community.dto.UpdateRotationChartDTO;
import com.xyoto.community.entity.main.SelectedRotationChart;
import com.xyoto.community.mapper.main.SelectedRotationChartMapper;
import com.xyoto.community.service.SelectedRotationService;
import com.xyoto.community.utils.PictureUtils;
import com.xyoto.community.utils.Result;
import com.xyoto.community.utils.XYOtoOssUtils;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author coderFu
 * @create 2022/2/17
 */
@Service
@Slf4j
public class SelectedRotationServiceImpl implements SelectedRotationService {

    private final Logger logger = LoggerFactory.getLogger(SelectedRotationServiceImpl.class);

    @Autowired
    private XYOtoOssUtils ossUtils;

    @Autowired
    private SelectedRotationChartMapper selectedRotationChartMapper;


    @Override
    public Result addSelectedRotation(AddSelectedRotationDTO addSelectedRotationDTO) {

        SelectedRotationChart selectedRotationChart = new SelectedRotationChart();
        MultipartFile file = addSelectedRotationDTO.getFile();
        if (file != null) {
            try {
                String path = ossUtils.uploadFileAvatar(file, OssBucketModuleName.HOME_SELECTED_ROTATION_CHART_PICTURE);
                selectedRotationChart.setPath(path);
            } catch (IOException e) {
                e.printStackTrace();
                logger.info("上传精选轮播图失败");
                return Result.error("上传精选轮播图失败");
            }
        }
        // 判断新增的轮播图是否需要展示
        Integer status = addSelectedRotationDTO.getStatus();
        Integer orders = addSelectedRotationDTO.getOrders();
        selectedRotationChart.setStatus(status);
        if (status == RotationChartShowStatus.NOT_SHOW) {
            // 轮播图不展示，直接存储到数据库中
            selectedRotationChart.setOrders(orders);
        } else {
            //查询当前正在展示的轮播图顺序的最大值,限定新增轮播的范围
            Integer count = selectedRotationChartMapper.getMaxShowSelectedRotationOrders();
            if (count == null){
                count = 0;
            }
            // 轮播图需要展示，考虑是否存在展示顺序冲突问题

            SelectedRotationChart selectedRotation = selectedRotationChartMapper.getSelectedRotationByOrdersAndStatus(orders, RotationChartShowStatus.SHOW);
            if (selectedRotation == null) {
                // 判断是否需要调整用户设定的轮播顺序
                if (orders > (count + 1)) {
                    // 设定为最大值加一
                    orders = count + 1;
                }
            } else {
                // 出现冲突，需要调整
                // 切换顺序
                orders = selectedRotation.getOrders();
                selectedRotation.setOrders(count + 1);
                Integer result = selectedRotationChartMapper.updateSelectedRotationOrders(selectedRotation);
                if (result < OperationStatus.SUCCESS) {
                    logger.info("更新精选轮播图失败");
                    return Result.error();
                }
            }
            System.out.println("到这里了吗");
            selectedRotationChart.setOrders(orders);
            selectedRotationChart.setLink(addSelectedRotationDTO.getLink());
            Integer result = selectedRotationChartMapper.addSelectedRotationChart(selectedRotationChart);
            if (result < OperationStatus.SUCCESS) {
                logger.info("添加精选轮播图失败");
                return Result.error();
            }
        }
        return Result.success();
    }

    @Override
    public Result updateSelectedRotation(UpdateRotationChartDTO updateRotationChartDTO) {

        // 查询之前的轮播图信息，以便后续删除存储的图片
        SelectedRotationChart selectedRotationChart = selectedRotationChartMapper.getSelectedRotationById(updateRotationChartDTO.getId());
        // 判断是否有图片的更新
        MultipartFile file = updateRotationChartDTO.getFile();
        if (file != null) {
            //上传新的轮播图
            try {
                String path = ossUtils.uploadFileAvatar(file, OssBucketModuleName.HOME_SELECTED_ROTATION_CHART_PICTURE);
                // 删除之前的图片信息
                ossUtils.deleteFileAvatar(PictureUtils.addPrefix(selectedRotationChart.getPath()));
                selectedRotationChart.setPath(path);
            } catch (IOException e) {
                e.printStackTrace();
                logger.info("精选轮播图上传OSS失败");
                return Result.error();
            }
        }
        //判断是否是下架轮播图
        Integer status = updateRotationChartDTO.getStatus();
        Integer orders = updateRotationChartDTO.getOrders();
        if (status == RotationChartShowStatus.SHOW) {
            // 判断顺序是否需要调整
            if (orders != null) {
                //查询当前最大的轮播图展示顺序范围
                Integer count = selectedRotationChartMapper.getMaxShowSelectedRotationOrders();
                // 判断是否存在展示顺序冲突问题
                SelectedRotationChart selectedRotation = selectedRotationChartMapper.getSelectedRotationByOrdersAndStatus(orders, RotationChartShowStatus.SHOW);
                if (selectedRotation == null) {
                    // 不存在冲突， 限定轮播图的范围
                    if (orders > (count + 1)) {
                        orders = count + 1;
                    }
                } else {
                    // 存在冲突， 处理冲突
                    selectedRotation.setOrders(count + 1);
                    Integer result = selectedRotationChartMapper.updateSelectedRotationOrders(selectedRotation);
                    if (result < OperationStatus.SUCCESS) {
                        logger.info("精选轮播图更新顺序失败");
                        return Result.error();
                    }
                }
            }
        }
        selectedRotationChart.setOrders(orders);
        selectedRotationChart.setLink(updateRotationChartDTO.getLink());
        selectedRotationChart.setStatus(updateRotationChartDTO.getStatus());
        Integer result = selectedRotationChartMapper.updateSelectedRotationOrders(selectedRotationChart);
        if (result < OperationStatus.SUCCESS) {
            logger.info("更新精选轮播图失败");
            return Result.error();
        }
        return Result.success();
    }

    @Override
    public Result getShowSelectedRotationList() {
        return Result.success(selectedRotationChartMapper.getShowSelectedRotationList(RotationChartShowStatus.SHOW));
    }


}
